//
//  AppListViewController.m
//  LimitFree
//
//  Created by LZXuan on 15-6-8.
//  Copyright (c) 2015年 轩哥. All rights reserved.
//

#import "AppListViewController.h"
#import "AFNetworking.h"
#import "AppModel.h"
#import "AppCell.h"



@interface AppListViewController ()

@end

@implementation AppListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //导航条背景
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed: @"navigationbar"] forBarMetrics:UIBarMetricsDefault];
    //设置导航基本内容
    [self addTitleViewWithTitle:self.title];
    [self addItemWithTitle:@"分类" target:self action:@selector(leftClick:) isLeft:YES];
    [self addItemWithTitle:@"设置" target:self action:@selector(rightClick:) isLeft:NO];
    
    [self creatTableView];
    
    //创建af
    _manager = [AFHTTPRequestOperationManager manager];
    //响应格式 二进制 不解析
    _manager.responseSerializer  = [AFHTTPResponseSerializer serializer];
}

#pragma mark - tableView
- (void)creatTableView {
    self.dataArr = [[NSMutableArray alloc] init];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenSize.width, kScreenSize.height-64-49) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    //self.tableView.rowHeight = 100;
    //注册cell
    [self.tableView registerNib:[UINib nibWithNibName:@"AppCell" bundle:nil] forCellReuseIdentifier:@"AppCell"];

    [self.view addSubview:self.tableView];
}
#pragma mark - tableView协议
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AppCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AppCell" forIndexPath:indexPath];
    AppModel *model = self.dataArr[indexPath.row];
    //传分类
    cell.category = self.category;
    //填充cell
    [cell showDataWithModel:model indexPath:indexPath];
    return cell;
}


#pragma mark - 如果不满足子类 那么子类要重写

//
//第一次下载
- (void)firstDownload {//在子类调用
    self.currentPage = 1;
    self.isRefreshing = NO;
    self.isLoadMoring = NO;
    self.categoryId = @"0";
    //下载数据
    [self addTaskUrl:self.requestUrl isRefresh:YES];
}
//增加下载任务
- (void)addTaskUrl:(NSString *)url isRefresh:(BOOL)isRefresh {
    if ([self.category isEqualToString:kHotType]) {
        //热榜接口和其他接口处理不一样
        url = [NSString stringWithFormat:url,self.currentPage];
    }else{
        url = [NSString stringWithFormat:url,self.currentPage,self.categoryId];
    }
    __weak typeof (self) weakSelf = self;
    //开始下载
    [_manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"下载成功");
        if (responseObject) {
            //解析json
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            NSArray *arr = dict[@"applications"];
            //遍历数组解析
            for (NSDictionary *appDict in arr) {
                AppModel *model = [[AppModel alloc] init];
                //kvc 赋值
                [model setValuesForKeysWithDictionary:appDict];
                [weakSelf.dataArr addObject:model];
            }
            //刷新表格
            [weakSelf.tableView reloadData];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"下载失败");
    }];
}

//刷新
- (void)creatRefreshView {//在子类中调用 如果不适合子类那么重写

}
//结束刷新
- (void)endRefreshing {

}




- (void)leftClick:(UIButton *)button {

}
- (void)rightClick:(UIButton *)button {
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
